﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Service.Core.Interface {

    /// <summary>
    /// Signs-Baseservice-Aop接口
    /// </summary>
    public interface ISigns {

        /// <summary>
        /// 设置跟踪器
        /// </summary>
        /// <param name="tracker"></param>
        void SetTracker(ITracker tracker);
    }

    /// <summary>
    /// ISigns的监听接口
    /// </summary>
    public interface ISignsListener
    {

    }

    /// <summary>
    /// 推送运行时数据
    /// </summary>
    public interface IRunTimePush {

        void Push(ITracker tracker, params object[] param);

        void RunTimer();

    }

    /// <summary>
    /// 跟踪载体
    /// </summary>
    public interface ITracker {

        /// <summary>
        /// 设置处理类型
        /// </summary>
        void SetHandlerType(int type);

        /// <summary>
        /// 获取处理类型
        /// </summary>
        /// <returns></returns>
        int GetHandlerType();
    }

    public class ThreadLocal {

        public static readonly IRunTimePush Pusher;

        static ThreadLocal() {
            Pusher = new DbPusher(true);
            new Thread(Pusher.RunTimer).Start();
        }

        /// <summary>
        ///  Debug
        /// </summary>
        /// <param name="trackerid"></param>
        /// <param name="rpcid"></param>
        /// <param name="content"></param>
        public static void Debug(string trackerid, string rpcid, object content) {
            var tracker = Tracker.GenTracker(trackerid, rpcid);
            tracker.HandlerType = Tracker.Debug;

            Pusher.Push(tracker, content);
        }



        public static void Push(HttpRequestBase request, int handerType, params object[] param) {

            var tracker = Tracker.GenTracker(GetTrackerId(request), GetRpcId(request));
            tracker.HandlerType = handerType;
            Pusher.Push(tracker, param);
        }


        public static void Push(ITracker tracker, params object[] param) {
            if (tracker == null) return;
            Pusher.Push(tracker, param);
        }



        /// <summary>
        /// 设置跟踪信息
        /// </summary>
        /// <param name="trackId">模块id</param>
        /// <param name="request">当前Http请求</param>
        public static Tracker InitTracker(string trackId, HttpRequest request) {
            var tracker = Tracker.GenTracker(trackId, Guid.NewGuid().ToString());
            request.Headers["trackId"] = tracker.Id;
            request.Headers["rpcId"] = tracker.RpcId;
            return tracker;
        }

        /// <summary>
        /// 用户接口初始化
        /// Controller
        /// </summary>
        /// <param name="trackId">模块id</param>
        /// <param name="request">当前Http请求</param>
        /// <param name="services">service对象</param>
        public static void InitTracker(string trackId, HttpRequest request, params ISigns[] services) {
            foreach (var service in services) {
                service.SetTracker(InitTracker(trackId, request));
            }
        }



        /// <summary>
        /// 获取模块Id
        /// </summary>
        /// <param name="request">当前Http请求</param>
        /// <returns></returns>
        public static string GetTrackerId(HttpRequestBase request) {
            return request.Headers["trackId"];
        }

        /// <summary>
        /// 获取上下文ID
        /// </summary>
        /// <param name="request">当前Http请求</param>
        /// <returns></returns>
        public static string GetRpcId(HttpRequestBase request) {
            return request.Headers["rpcId"];
        }


    }

}
